package com.cy.pj.notice.web.interceptor;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalTime;


public class TimeAccessInterceptor implements HandlerInterceptor {
    private static final Logger log = LoggerFactory.getLogger(TimeAccessInterceptor.class);

    /**
     * @param request  请求对象
     * @param response 响应对象
     * @param handler  目标处理器对象
     * @return 是否对请求进行放行，继续执行handler方法
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        LocalTime localTime = LocalTime.now();
        int hour = localTime.getHour();
        log.info("==TimeAccessInterceptor.preHandle==Time:{}", localTime);
//        if (hour>9||hour<18){
//            throw new ServiceException("9:00-16:00禁止访问");
//        }
        return true;//true放行，false请求到此结束
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}














